package com.eds.controller;

import com.eds.dto.BackBoardDTO;
import com.eds.dto.RegisterRequestDTO;
import com.eds.exception.BusException;
import com.eds.pojo.BaseResult;
import com.eds.pojo.CodeEnum;
import com.eds.service.TeacherService;
import com.eds.service.UserService;
import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName：userController
 * @Author: vvSmile
 * @Date: 2025/03/14
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@RestController
@RequestMapping("/userController")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private TeacherService teacherService;

    //1. 账号密码登录
    @PostMapping("/login")
    public BaseResult loginPassword(@RequestBody User user) {
        BaseResult result = userService.loginByUsernamePassword(user);
        if (result == null)
            return BaseResult.error(CodeEnum.LOGIN_ERROR);
        result.setMessage("登录成功");
        return result;
    }

    //2.注册
    @PostMapping("/register")
    public BaseResult registerController(@RequestBody RegisterRequestDTO registerRequestDTO) {
        //添加用户信息，返回用户Id
        Long registeredUserId = userService.registerService(registerRequestDTO.getUser());
        if (registerRequestDTO.getTeacher() != null) {
            //设置用户ID
            registerRequestDTO.getTeacher().setUserId(registeredUserId);
            //添加教师信息
            teacherService.registerService(registerRequestDTO.getTeacher());
        }
        if (registeredUserId == 0L)
            return BaseResult.error(CodeEnum.REGISTER_ERROR);
        return BaseResult.ok(CodeEnum.REGISTER_SUCCESS.getMessage(), CodeEnum.REGISTER_SUCCESS.getCode());
    }

    //3.发送注册验证码
    @GetMapping("/sendRegisterMessageCode")
    public BaseResult sendRegisterMessageCodeController(String mobile) {
        boolean sendMessageReturn = userService.sendRegisterMessageCodeService(mobile);
        if (!sendMessageReturn)
            return BaseResult.error(CodeEnum.SEND_SMS_MESSAGE_ERROR);
        return BaseResult.ok(CodeEnum.SEND_SMS_MESSAGE_SUCCESS.getMessage(), CodeEnum.SEND_SMS_MESSAGE_SUCCESS.getCode());
    }
    //4检查注册验证码
    @GetMapping("/checkRegisterMessageCode")
    public BaseResult checkRegisterMessageCodeController(String mobile,String userCode) {
        Boolean checkMessageReturn = userService.checkRegisterMessageCodeService(mobile, userCode);
        if (!checkMessageReturn)
            return BaseResult.error(CodeEnum.CHECK_SMS_MESSAGE_ERROR);
        return BaseResult.ok(CodeEnum.CHECK_SMS_MESSAGE_SUCCESS.getMessage(), CodeEnum.CHECK_SMS_MESSAGE_SUCCESS.getCode());
    }

    //5.发送登录验证码
    @GetMapping("/sendLoginMessageCode")
    public BaseResult sendLoginMessageCodeController(String mobile) {
        boolean sendMessageReturn = userService.sendLoginMessageCodeService(mobile);
        if (!sendMessageReturn)
            return BaseResult.error(CodeEnum.SEND_SMS_MESSAGE_ERROR);
        return BaseResult.ok(CodeEnum.SEND_SMS_MESSAGE_SUCCESS.getMessage(), CodeEnum.SEND_SMS_MESSAGE_SUCCESS.getCode());
    }
    //6.检查登录验证码
    @GetMapping("/checkLoginMessageCode")
    public BaseResult checkLoginMessageCodeController(String mobile,String userCode) {
        BaseResult result = userService.checkLoginMessageCodeService(mobile, userCode);
        if (result.getCode() != 200)
            return BaseResult.error(CodeEnum.CHECK_SMS_MESSAGE_ERROR);
        return result;
    }

    //7.忘记密码（修改密码）
    @PostMapping("/resetPassword")
    public BaseResult resetPasswordController(@RequestBody User user) {
        Boolean resetPasswordReturn = userService.resetPasswordService(user);
        if (!resetPasswordReturn)
            return BaseResult.error(CodeEnum.FAILED_RETRIEVE_PASSWORD);
        return BaseResult.ok(CodeEnum.SUCCESS_RETRIEVE_PASSWORD.getMessage(), CodeEnum.SUCCESS_RETRIEVE_PASSWORD.getCode());
    }

    //8.忘记密码发短信
    @GetMapping("/sendResetMessageCode")
    public BaseResult sendResetMessageCodeController(String mobile) {
        boolean sendMessageReturn = userService.sendResetMessageCodeService(mobile);
        if (!sendMessageReturn)
            return BaseResult.error(CodeEnum.SEND_SMS_MESSAGE_ERROR);
        return BaseResult.ok(CodeEnum.SEND_SMS_MESSAGE_SUCCESS.getMessage(), CodeEnum.SEND_SMS_MESSAGE_SUCCESS.getCode());
    }


    //9. 检查忘记密码验证码
    @GetMapping("/checkResetMessageCode")
    public BaseResult checkResetMessageCodeController(String mobile, String userCode) {
        Boolean checkMessageReturn = userService.checkResetMessageCodeService(mobile, userCode);
        if (!checkMessageReturn)
            return BaseResult.error(CodeEnum.CHECK_SMS_MESSAGE_ERROR);
        return BaseResult.ok(CodeEnum.CHECK_SMS_MESSAGE_SUCCESS.getMessage(), CodeEnum.CHECK_SMS_MESSAGE_SUCCESS.getCode());
    }

    //10.获取用户名
    @GetMapping("/getUserName")
    public BaseResult getName(@RequestHeader("authorization") String token) {
        User userLoginUsernameService = userService.getUserLoginUsernameService(token);
        if (userLoginUsernameService == null) {
            return BaseResult.error(CodeEnum.GET_USERNAME_ERROR);
        }
        return BaseResult.ok("获取用户名成功！", userLoginUsernameService);
    }

    //11.获取用户全部信息
    @GetMapping("/getUserDetailsInfo")
    public BaseResult getUserDetailsInfo(@RequestHeader("authorization") String token) {
        User userDetailsInfoService = userService.getUserDetailsInfoService(token);
        if (userDetailsInfoService == null) {
            return BaseResult.ok(CodeEnum.SELECT_USER_DETAILS_ERROR);
        }
        return BaseResult.ok("获取用户详细信息成功", userDetailsInfoService);
    }

    //12.修改用户信息（不能修改手机号和密码）
    @PostMapping("/updateUserInfo")
    public BaseResult updateUserInfo(@RequestHeader("authorization") String token, @RequestBody User user) {
        Boolean updateUserDetailsInfoService = userService.updateUserInfo(token, user);
        if (updateUserDetailsInfoService == null) {
            return BaseResult.ok(CodeEnum.UPDATE_USER_INFO_ERROR);
        }
        return BaseResult.ok("修改信息成功", updateUserDetailsInfoService);
    }

    //13.修改手机号
    @PostMapping("/updateUserPhone")
    public BaseResult updateUserPhone(@RequestHeader("authorization") String token, @RequestBody User user) {
        Boolean updateUserPhoneService = userService.updateUserPhone(token, user);
        if (updateUserPhoneService == null) {
            return BaseResult.ok(CodeEnum.UPDATE_USER_INFO_ERROR);
        }
        return BaseResult.ok("修改信息成功", updateUserPhoneService);
    }


    //14.用户头像上传
    @PostMapping("/upload")
    public BaseResult upload(@RequestHeader("authorization") String token, @RequestParam("file") MultipartFile file) {
        BaseResult uploadReturn = userService.upload(token, file);
        return uploadReturn;
    }

    //15.通过Token获取userID
    @PostMapping("/getUserIdFromToken")
    public Long getUserIdFromToken(@RequestHeader("authorization") String token) {
        Long id = userService.getUserIdFromToken(token);
        if (id == null) {
            throw new BusException(CodeEnum.GET_USERID_BY_TOKEN.getCode(), CodeEnum.GET_USERID_BY_TOKEN.getMessage());
        }
        return id;
    }

    //999.测试
    @GetMapping("/test")
    public BaseResult getName() {
        BaseResult test = userService.test();
        return BaseResult.ok(test);
    }

    //11.获取用户全部信息
    @GetMapping("/getUserInfo")
    public User getUserInfo(@RequestHeader("authorization") String token) {
        User userDetailsInfoService = userService.getUserDetailsInfoService(token);
        return userDetailsInfoService;
    }

    //12.通过用户Id查询用户信息
    @GetMapping("/getUserInfoByTeacherId")
    public User getUserInfoByTeacherId(Long userId) {
        User getUserInfoByUserId = userService.getUserInfoByUserId(userId);
        return getUserInfoByUserId;
    }

    //13.通过用户Id查询用户信息
    @GetMapping("/getUserByUserId")
    public User getUserByUserId(Long userId) {
        User getUserInfoByUserId = userService.getUserByUserId(userId);
        return getUserInfoByUserId;
    }

    //14.Token校验
    @GetMapping("/verifyToken")
    public BaseResult verifyToken(@RequestHeader("authorization") String token, Long userId) {
        Boolean verifyTokenReturn = userService.verifyToken(token, userId);
        if (verifyTokenReturn) {
            return BaseResult.ok("用户匹配！", verifyTokenReturn);
        }
        return BaseResult.error(CodeEnum.VERIFY_TOKEN_ERROR);
    }

    //15.查询所有老师的所有课程（Ai推荐使用）
    @GetMapping("/getAllTeachersCourses")
    public List<User> getAllTeachersCourses() {
        List<User> allTeachers = userService.getAllTeachersCourses();
        return allTeachers;
    }

    //获取所有用户
    @GetMapping("/getAllUser")
    List<User> getAllUser() {
        return userService.getAllUser();
    }

    //通过用户名获取头像
    @GetMapping("/getUserAvatarByName")
    public BaseResult getUserAvatarByName(String userName) {
        String avatar = userService.getUserAvatarByName(userName);
        if (avatar == null)
            return BaseResult.error(CodeEnum.GET_USER_AVATAR_ERROR);
        return BaseResult.ok("获取用户头像成功！", avatar);
    }

    //获取所有用户
    @GetMapping("/getAllUsers")
    public BaseResult getAllUsers(String userName) {
        return userService.getAllUsers(userName);
    }

    //修改用户角色
    @PutMapping("/updateUserRole")
    public BaseResult updateUserRole(@RequestBody User user) {
        return userService.updateUserRole(user);
    }

    //删除用户
    @DeleteMapping("/deleteUser")
    public BaseResult deleteUser(@RequestBody User user) {
        return userService.deleteUser(user);
    }

    //board
    @GetMapping("/getUserBoard")
    public BackBoardDTO getUserBoard() {
        return userService.getUserBoard();
    }


    @GetMapping("/getTodayUserCount")
    public List<User> getTodayUserCount() {
        return userService.getTodayUserCount();
    }
}
